
angular.module("myKitchen")
.controller("homeController",["$scope","$state","homeService","$timeout",function($scope,$state,homeService,$timeout){
	homeService.getCarousel(function(res){
		//轮播图
		$scope.carouselData = res.data.carousel;
		$timeout(function(){
			$scope.carousel();
		},100)
		$scope.carousel = function(){
			var mySwiper = new Swiper(".swiper-container",{
				pagination : '.swiper-pagination',
				autoplay: 5000,
				paginationClickable: true,
				touchRatio:1,
				longSwipesRatio: 0.3,
				observer:true,
				observeParents:true
			})
		}

		//2017一起吃--导航
		$scope.navData = res.data.recommendedContent[0].items;
	})

	//主体数据
	homeService.getGoodItems(function(res){
		$scope.goodItemsData1 = res.data.goodItemsList1
		$scope.goodItemsData2 = res.data.goodItemsList2
		$scope.goodItemsData3 = res.data.goodItemsList3
	})

	$scope.open = function(url){
		console.log(url)
		window.location = url
	}
	$scope.open1 = function(url){
		url = "http://img.i-kitchen.cn/upload/" + url + ".jpg";
		window.location = url
	}

	$scope.addGoodItems = function(item){
		item.count = item.count ? (item.count + 1) : 1;
		homeService.addGoodItems(item)
	}


	$scope.getCarouselItemsData = function(itemTitle){
		homeService.addCarouselItemTitle(itemTitle);
	}

}])
.controller("homeCarouselController",function($scope,homeService,$timeout){
	homeService.getCarouselItems(function(res){
		$scope.carouselItemsData = res
		for (var i = res.length - 1; i >= 0; i--) {
			if(res[i].data.aTName == homeService.sendCarouselItemTitle()){
				$scope.carouselItems = res[i]
			}
		}
	})

	$scope.addCarouselItems = function(item){
		$scope.bol = false;
		$scope.bol = !($scope.bol);
		if($scope.bol == true){
			$timeout(function(){
				$scope.bol = false;
			}, 500);
		}

		item.count = item.count ? (item.count + 1) : 1

		homeService.addCarouselItems(item)

	}

})
.config(function($stateProvider,$urlRouterProvider){
	$stateProvider.state("carouselItems",{
		url:"/carouselItems",
		templateUrl:"view/second/carouselItems.html",
		controller:"homeCarouselController"
	})
})